//
//  JDNewAuthenticationView.swift
//  DragonWallet
//
//  Created by volantPig on 2025/1/14.
//

import UIKit
import ProgressHUD

class JDNewAuthenticationView: UIView {
    
    var confirmButtonSelected: ((String,String)->Void)?
    
    var containerView: UIView?
    var backButton: UIButton?
    var confirmButton: UIButton?
    var titleLabel: UILabel?
    var hintLabel: UILabel?
    
    var nameInputView: UITextField?
    var idCardInputView: UITextField?
    
    
    // YES: 提现 实名认证   NO: 购物 实名认证
    var isUpdate: Bool = false
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, screenWidth, screenHeight))
        self.setupConfig()
        self.setupUIElements()
    }
    
    func setupConfig() {
        self.backgroundColor = color("#000000").withAlphaComponent(0.4)
    }
    
    func setupUIElements() {
        self.containerView = UIView()
        self.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.right.bottom.equalTo(self)
            
        })
        self.containerView?.backgroundColor = .white
        
        self.backButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.backButton!)
        self.backButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!.snp.centerX).offset(-5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.backButton?.layer.cornerRadius = 27
        self.backButton?.backgroundColor = color("#F5F6F8")
        self.backButton?.setTitle("取消", for: .normal)
        self.backButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.backButton?.setTitleColor(.black, for: .normal)
        self.backButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        
        self.confirmButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.confirmButton!)
        self.confirmButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.left.equalTo(self.containerView!.snp.centerX).offset(5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.confirmButton?.setTitle("确定", for: .normal)
        self.confirmButton?.layer.cornerRadius = 27
        self.confirmButton?.backgroundColor = color("#333333")
        self.confirmButton?.setTitleColor(.white, for: .normal)
        self.confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.confirmButton?.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        
        
        self.hintLabel = UILabel()
        self.containerView?.addSubview(self.hintLabel!)
        self.hintLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.bottom.equalTo(self.backButton!.snp.top).offset(-12)
            make.height.equalTo(30)
        })
        self.hintLabel?.backgroundColor = color("#FFF9F9")
        self.hintLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#FA6161"), numberOfLines: 1)
        self.hintLabel?.text = "确认授权小尨包及与本人交易的商户使用身份"
        
        self.idCardInputView = UITextField()
        self.containerView?.addSubview(self.idCardInputView!)
        self.idCardInputView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.bottom.equalTo(self.hintLabel!.snp.top).offset(-40)
            make.height.equalTo(58)
        })
        self.idCardInputView?.backgroundColor = .commonColor()
        self.idCardInputView?.placeholder = "请输入身份证号码"
        
        
        let idCardLeftView = JDPaddingLabel()
        idCardLeftView.text = "身份证号 *"
        idCardLeftView.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .medium), textColor: color("#333333"), numberOfLines: 1)
        idCardLeftView.edgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 15)
        
        self.idCardInputView?.leftView = idCardLeftView
        self.idCardInputView?.leftViewMode = .always
        
        
        self.nameInputView = UITextField()
        self.containerView?.addSubview(self.nameInputView!)
        self.nameInputView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.bottom.equalTo(self.idCardInputView!.snp.top).offset(-10)
            make.height.equalTo(58)
        })
        self.nameInputView?.backgroundColor = .commonColor()
        self.nameInputView?.placeholder = "请输入用户名"
        
        
        let nameLeftView = JDPaddingLabel()
        nameLeftView.text = "真实姓名 *"
        nameLeftView.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .medium), textColor: color("#333333"), numberOfLines: 1)
        nameLeftView.edgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 15)
        
        self.nameInputView?.leftView = nameLeftView
        self.nameInputView?.leftViewMode = .always
        
        
        
        self.titleLabel = UILabel()
        self.containerView?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!)
            make.right.equalTo(self.containerView!)
            make.bottom.equalTo(self.nameInputView!.snp.top).offset(-26)
            make.top.equalTo(self.containerView!).offset(30)
        })
        self.titleLabel?.text = "新增实名认证信息"
        self.titleLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 18, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func cancelAction() {
        self.removeFromSuperview()
    }
    
    @objc func confirmAction() {
        
        if self.isUpdate {
            self.updateAuthenticationAction()
        } else {
            self.addNewAuthenticationAction()
        }
        
    }
    
    
    func updateAuthenticationAction() {
        
        
        if (self.nameInputView?.text?.count ?? 0) <= 0 {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "请输入用户名")
        }
        if (self.idCardInputView?.text?.count ?? 0) <= 0 {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "请输入身份证号码")
        }
        
        var params = [String: Any]()
        params["realName"] = self.nameInputView?.text
        params["certNo"] = self.idCardInputView?.text
        ProgressHUD.animate()
        JDNetWorkRequest(.updateUserRealNameById(params)) { responseStr in
            ProgressHUD.dismiss()
            printLog("认证接口请求成功:\(responseStr)")
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                self.cancelAction()
//                var nameInputView: UITextField?
//                var idCardInputView: UITextField?
                self.confirmButtonSelected?(self.nameInputView?.text ?? "", self.idCardInputView?.text ?? "")
            } else {
                JDTools.getKeyWindows()?.rootViewController?.view.toast(message: commonModel?.msg ?? "网络错误")
            }
        } failure: { error in
            ProgressHUD.dismiss()
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: error.localizedDescription)
        } error: { errorCode in
            ProgressHUD.dismiss()
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "网络异常")
        }

    }
    
    
    
    
    
    func addNewAuthenticationAction() {
        print("新增实名认证信息")
        
        if (self.nameInputView?.text?.count ?? 0) <= 0 {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "请输入用户名")
        }
        if (self.idCardInputView?.text?.count ?? 0) <= 0 {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "请输入身份证号码")
        }
        
        var params = [String: Any]()
        params["realName"] = self.nameInputView?.text
        params["idCardNo"] = self.idCardInputView?.text
        
        JDNetWorkRequest(.saveRealNameIdentificationInfo(params)) { responseStr in
            
            printLog("认证接口请求成功:\(responseStr)")
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                self.cancelAction()
                self.confirmButtonSelected?(self.nameInputView?.text ?? "", self.idCardInputView?.text ?? "")
            } else {
                JDTools.getKeyWindows()?.rootViewController?.view.toast(message: commonModel?.msg ?? "网络错误")
            }
    
            
        } failure: { error in
            
        } error: { errorCode in
            
        }

    }
    
    
}
